﻿#include "Enemy.h"
#include "GameUtil.h"

using namespace cocos2d;

Enemy::Enemy(void)
{
	d_shakeActionTag = kCCActionTagInvalid;
}


Enemy::~Enemy(void)
{
}

bool Enemy::init(){  
    if (!Creature::init()) {  
        return false;  
    }  

	// init
	d_objType = OBJ_TYPE_ENEMY;
	d_state = STATE_DEAD;
	d_creatureSprite = CCSprite::create();

	// bullet
	d_bulletSpeed = 100;
	d_bulletDamage = 10;
	d_bulletType = BULLET_TYPE_1;
	d_shootInterval = 100;
	
	// hp
	d_maxHP = 100;

	// move
	d_speed = 100;
	d_stopPos = 40;

    return true;  
} 

void Enemy::afterInit()
{
	// param
	d_speed = d_setting->getSpeed(d_speed, this);
	d_stopPos *= d_winSize.width/100;

    Creature::afterInit();
}

CCFiniteTimeAction* Enemy::getMoveAction(){
	float duration = (this->getPositionX() - d_stopPos) / d_speed;
	CCFiniteTimeAction* action = CCMoveTo::create(duration, ccp(d_stopPos, this->getPositionY()));
	return action;
}

void Enemy::spawn(CCPoint pos)
{
	CCSize size =  this->getContentSize();

	// set start position
	this->setPositionX(d_winSize.width + size.width/2 + ((pos.x-1) * size.width));
	this->setPositionY(GameUtil::getCreatureY(pos.y));

	this->d_state = STATE_MOVE;
	this->setVisible(true);

	// walk
	d_creatureSprite->runAction(this->getWalkAction());

	// move
	this->runAction(
		CCSequence::create(
			this->getMoveAction(),
			CCCallFuncN::create(this, callfuncN_selector(Enemy::moveEnd)), 
			NULL));
}

void Enemy::moveEnd(CCNode* sender)
{
	this->d_state = STATE_ATTACK;
	this->stopAllActions();
	d_creatureSprite->stopAllActions();

	this->startShoot(false);
}
 
float Enemy::onHit(float damage)
{
	float rate = GameUtil::instance()->getSetting()->getDeadlyShootRate();
	int rand = GameUtil::instance()->random(0, 99, 1);
	if (rand < rate * 100) {
		int realDagame = damage * GameUtil::instance()->getSetting()->getDeadlyShootPower();
		// animation
		char lostHp[20];
		sprintf(lostHp, "%d", realDagame);

		CCLabelAtlas* pCostHp = CCLabelAtlas::create(lostHp, "number_02.png", 26, 35, '0');
		//CCLabelTTF* pCostHp = CCLabelTTF::create(lostHp, "Arial", 50);
		pCostHp->setColor(ccc3(0x99,0x0,0x56));	// color purple
		pCostHp->setPosition(ccp(this->getPosition().x, this->getPosition().y + this->getContentSize().height/2));
		CCFiniteTimeAction* showDamage = CCMoveTo::create(0.4f, ccp(this->getPosition().x, this->getPosition().y + this->getContentSize().height/2 + 40));
		CCFiniteTimeAction* showDamageDone = CCCallFuncN::create(this, callfuncN_selector(Enemy::showDamageDone));

		GameUtil::instance()->getGameStateLayer()->addChild(pCostHp);
		pCostHp->runAction(CCSequence::create(showDamage, showDamageDone, NULL));

		return realDagame;	
	}
	return damage;
}

void Enemy::showDamageDone(CCNode* sender)
{
	sender->removeFromParent();
	//sender->release();
}

void Enemy::onDead()
{
	GameUtil::instance()->getGameStateLayer()->getMoney(this->getMoney());
	GameUtil::instance()->getEnemyManager()->removeEnemy(this);
	GameUtil::instance()->getWaveManager()->killed(this->getMoney(), this->getMaxHP());
}

CCFiniteTimeAction* Enemy::getDieAction()
{
	// shake todo
	CCLayer* gameLayer = GameUtil::instance()->getGameLayer();
	if(kCCActionTagInvalid != d_shakeActionTag)
	{
		gameLayer->stopActionByTag(d_shakeActionTag);
	}
	CCAction *act = CCSequence::create(
			CCShaky3D::create(0.4f, CCSizeMake(15,10), 5, false),
			CCStopGrid::create(),
			NULL);
	d_shakeActionTag = act->getTag();
	gameLayer->runAction(act);

	return CCSpawn::create(
		CCScaleTo::create(0.8f, .0f, .0f),
		CCEaseIn::create(CCRotateTo::create(0.8f, 1800), 3.0f),
        NULL);
}